<template>
  <div class="left">
    <div class="title">
      <div  :class="state.activeTab=== 0 ? 'titleItem active' : 'titleItem' " @click="actice(0)">任务表</div>
      <div  :class="state.activeTab=== 1 ? 'titleItem active' : 'titleItem' " @click="actice(1)">进行中</div>
      <div  :class="state.activeTab=== 2 ? 'titleItem active' : 'titleItem' " @click="actice(2)">已完成</div>
    </div>
    <div class="content">
      <div class="contentArr">

        <div class="contentItem" v-for="(item,index) in state.tableData" :key="index">
          <div class="zz" @click="goDetail(item.id,item.rec_id)"></div>
          <div class="icon" :style="{'background':item.recStatus===0?'#597EF7':'#36CFC9'}">Task</div>
          <div class="text">
            <div class="textTitle">{{item.title}}</div>
            <div class="desc">{{item.data}}</div>
          </div>
          <div class="integralIcon">
            <img src="@/assets/img/jifen.png" v-if="item.type == 2">
            <img src="@/assets/img/yue.png" v-if="item.type == 1">
            <span v-if="item.type == 2">{{item.integral}}积分</span>
            <span v-if="item.type == 1">{{item.money}}余额</span>
          </div>
          <div class="btn" @click="goSeekHelpDetail(index)">
            <span v-if="item.recStatus=== 0" class="taskBtn">领取任务</span>
            <span v-if="item.recStatus=== 1" class="taskingBtn" >进行中</span>
            <span v-if="item.recStatus=== 2" class="taskingBtn" >审核中</span>
            <span v-if="item.recStatus=== 3" class="taskingBtn" >已通过</span>
          </div>

        </div>
      </div>
      <div v-if="state.tableData.length === 0" class="el-center">
        <div class="home">
          <svg class="icon-svg banner-img" aria-hidden="true">
            <use xlink:href="#icon-home-banner"></use>
          </svg>
          <div class="color">空空如也~</div>
        </div>
      </div>
      <div class="foot" v-else>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="state.total"
          :page-size="state.pagesize"
          :current-page="state.currentPage"
          @current-change="currentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  // vue3 弃用了beforeCreate created
  import {getCurrentInstance,ref,reactive,onMounted,onUpdated,onUnmounted} from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  const state = reactive({
    activeTab : 0,
    tableData: [
    ],
    total: 0,
    currentPage: 1,
    pagesize: 8,
    page: 1,
    router: ''
  });
  function handleClick(tab, event){
   // console.log(tab, event);
  };
  export default {
    name: 'userTask',
    components: {},
    props: {
      type: {
        type: String,
        default: '1'
      }
    },
    watch: {
      '$route' (to, from) {
          console.log('执行')
      }
    },
    setup(props,{emit}){
      const ctx = getCurrentInstance();
      const _this = ctx.appContext.config.globalProperties
      const router = useRouter();

      function goDetail(index,rec_id){
        router.push({
          name:'basicListDetail',
          query:{
            id: index,
            rec_id:rec_id
          }
        })
      };
      //获取任务列表
      const taskList = () => {
        _this.$request({
          url: "?s=Learn.Task.GetTask",
          params: {page:state.page,pageSize:state.pagesize,status:1,task_type:props.type},
        })
          .then((res) => {
            if(state.page == 1){
              state.tableData = []
            }
            state.tableData = res.data.data
            state.total = res.data.total
            state.router = router.currentRoute.value.name
          })
      }
      const currentChange = (val) => {
        if (val !== state.page) {
          state.page = val
          return taskList()
        }
      }
      const goSeekHelpDetail = (index) => {
        let status = state.tableData[index].recStatus
        let id = state.tableData[index].id
        if(status === 0){
          //领取
          return receive(id,index);
        }
      }
      //领取
      const receive = (id,index) => {
        _this.$request({
          url: "?s=Learn.Task.RecTask",
          params: {task_id:id},
        })
          .then((res) => {
            if(res.ret === 200){
              state.tableData[index].recStatus = 1
              state.tableData[index].rec_id = res.data
              ElMessage.success({
                message: '领取成功',
                type: 'success'
              })
            }else{
              ElMessage.error(res.msg);
            }
          })
      }
      const actice = (index = undefined) => {
        if(index!==undefined) {
          state.activeTab = index
        }else{
          state.activeTab = 0
        }
        let type = state.activeTab
        state.page = 1

        if(type == 0){
          if(router.currentRoute.value.name == state.router && index === undefined){
              let isList = localStorage.getItem('isDetail')
              if(isList == 1){
                  localStorage.removeItem('isDetail')
                  return taskList()
              }else {
                //当前页路由刷新则阻止
                return
              }
          }
          return taskList()
        }else if(type == 1 || type == 2){
          return taskListOrder(type)
        }
      }
      //已领取 已完成
      const taskListOrder = (type) => {
        _this.$request({
          url: "?s=Learn.Task.GetRecTask",
          params: {page:state.page,pageSize:state.pagesize,status:state.activeTab,task_type:props.type},
        })
          .then((res) => {
            state.tableData = res.data.data
            state.total = res.data.total
          })
      }
      onMounted(actice);
      return{
        state,
        handleClick,
        router,
        goDetail,
        taskList,
        currentChange,
        goSeekHelpDetail,
        receive,
        actice
      }
    }
  }
</script>

<style lang="scss" scoped>
  .home {
    text-align: center;
    .banner-img{
      font-size: 364px;
    }
  }
  .color {
    color: #a0a7b5;
  }
  .left {
    flex: 2;
    background-color: #fff;
    height: 100%;
    margin-right: 24px;
  .title {
    display: flex;
    border-bottom: 1px solid #E9E9E9;
    box-sizing: border-box;
    align-items: center;
    height: 55px;
  .active{
  // border-bottom: 2px solid #1890FFFF;
    color: #1890FFFF !important;
    position: relative;
  }

  .active:after{
    content:'';
    position: absolute;
    bottom: -102%;
    left: 0%;
    height: 2px;
    width: 50px;
    background-color: #1890FFFF;
  }
  .titleItem {
    margin-left: 32px;
    font-size: 16px;
    font-weight: 600;
    color: #888888FF;
    cursor: pointer;
  }
  }

  .content {
    padding-left: 23px;
    padding-top: 12px;
    display: flex;
    flex-direction: column;
    height: calc(calc(100vh - 64px) - 40px - 70px - 55px);
  .contentArr{

    display: flex;
    flex-direction: column;
  .contentItem:nth-child(1) {
    border-top: 0px ;
  }
    .zz {
      position: absolute;
      top: 0;
      height: 100%;
      width: 80%;
    }
  .contentItem {
    position: relative;
    height: 100%;
    border-top: 1px solid #E9E9E9;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding: 10px;
  .icon {
    width: 48px;
    height: 48px;
    color: #fff;
    border-radius: 2px;
    background-color: #36CFC9FF;
    display: grid;
    place-items: center;
    font-weight: 600;
    font-size: 16px;
  }

  .text {
    cursor: pointer;
    margin-left: 24px;
    flex: 3;
    margin-right: 20px;
  .textTitle {
    color: #000000A6;
    font-weight: 600;
    word-break: break-all;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .desc {
    padding-top: 10px;
    color: #00000073;
    word-break: break-all;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  p {
    color: #00000073;
  }
  }

  .integralIcon{
    flex: 1;
    display: flex;
    align-items: center;
    padding-top: 18px;
  img{
    width: 22px;
    height: 22px;
  }
  span {
    color: #00000073;
    font-size: 14px;
    margin-left:2px;
  }
  }
  .btn {
    margin-right: 58px;

  .taskBtn{
    width: 74px;
    height: 32px;
    display: grid;
    place-items: center;
    color:#1890FFFF;
    border: 1px solid #1890FFFF;
    cursor: pointer;
    z-index: 2;
  }
  .taskingBtn{
    width: 74px;
    height: 32px;
    display: grid;
    place-items: center;
    color:#1890FFFF;
    border: 1px solid #1890FFFF;
    cursor: pointer;
    opacity: 0.5;
  }
  }
  }
  }

  .foot{
    flex: 1;
    margin-top: 10px;
    display: grid;
    place-items: center;
  }
  }
  }
</style>
